package com.igeekhome.ccs.biz.impl;

import com.igeekhome.ccs.biz.ICustomerFieldBiz;
import com.igeekhome.ccs.entity.CustomerField;
import com.igeekhome.ccs.entity.CustomerServiceGroup;
import com.igeekhome.ccs.mapper.CustomerFieldMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CustomerFieldBizImpl implements ICustomerFieldBiz {

//👇zyh----------------------------------------------------------------
    @Autowired
    CustomerFieldMapper customerFieldMapper;

    //拿到所有客户字段信息
    @Override
    public List<CustomerField> query() {
        return customerFieldMapper.query();
    }

    //模糊匹配
    @Override
    public List<CustomerField> queryCsByString(String str) {
        List<CustomerField> totalList = customerFieldMapper.query();
        List<CustomerField> resultList = new ArrayList<CustomerField>();

        str=str.trim();
        for(int i = 0 ; i<totalList.size(); i++) {
            //模糊匹配、大小写均可
            Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(totalList.get(i).getFieldName());
            if (matcher.find() ) {
                resultList.add(totalList.get(i));
            }
        }
        return resultList;
    }

    //添加字段
    @Override
    public int createCf(CustomerField customerField) throws ParseException {
        String str = customerField.getFieldName();
        str=str.trim();
        customerField.setFieldName(str);

        //创建时间
        Date now=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strNow = df.format(now);
        Date dateNow = df.parse(strNow);
        customerField.setCreateTime((java.sql.Date) dateNow);

        customerFieldMapper.createCf(customerField);
        return 0;
    }

    //删除字段
    @Override
    public int deleteCf(int fieldId) {
        customerFieldMapper.deleteCf(fieldId);
        return 0;
    }

    //编辑字段
    @Override
    public int editCf(int fieldId, String fieldName, boolean mustOrNot, String fieldType) {
        customerFieldMapper.updateCf(fieldId,fieldName,mustOrNot,fieldType);
        return 0;
    }
//👆zyh-----------------------------------------------------------------------
}
